<?php

/**
 * gry actions.
 *
 * @package    edmuch
 * @subpackage gry
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class gryActions extends sfActions
{

  

  public function executeIndex(sfWebRequest $request)
  {
    $this->games = Doctrine_Core::getTable('Game')
      ->createQuery('a')
      ->execute();
  }


  public function executeShow(sfWebRequest $request)
  {
    $this->forward404Unless($this->game = Doctrine_Core::getTable('Game')->find(array($request->getParameter('id'))), sprintf('Object game does not exist (%s).', $request->getParameter('id')));
    
    $userid = $this->getUser()->getGuardUser()->getId();
    $game_id = Doctrine_Core::getTable('Game')->find($this->getRequestParameter('id'))->getId();
    
    $this->rozgrywka_id = Doctrine_Core::getTable('GameResult')
      ->createQuery('r')
      ->select('r.*')
      ->where('r.user_id = ?', $userid)
      ->addWhere('r.games_id = ?', $game_id)
      ->addOrderBy('r.end_date DESC')  
      ->limit(1)
      ->fetchOne();
      
    $this->ratings = Doctrine_Core::getTable('GameRanking')
      ->createQuery('a')
      ->select('a.ocena_user, COUNT(a.ocena_user) as ilosc, AVG(a.ocena_user) as avg_rate')
      ->where('a.game_id = ?', $game_id)
      ->groupBy('a.ocena_user')
      ->execute();
    
  }
  
  public function executeOcena(sfWebRequest $request)
  {
      if ($this->getUser()->getGuardUser()->getId()) {
          $this->setLayout(false);
          
          $game = Doctrine_Core::getTable('Game')->find($this->getRequestParameter('id'));
          $ocena = $this->getRequestParameter('rate');
          $game_id = $game->getId();
          
          $dobazy = new GameRanking();
          $dobazy->user_id = $this->getUser()->getGuardUser()->getId();
          $dobazy->ocena_user = $ocena;
          $dobazy->game_id = $game_id;
          $dobazy->save();
      }      
      
      $this->redirect($this->generateUrl('gra', array('module' => 'gry', 'action' => 'show', 'id' => $game_id)));
  }

  public function executeZapis(sfWebRequest $request)
  {

      if ($this->getUser()->getGuardUser()->getId()) {
      
   $this->setLayout();
      
////////////////////////////////ZMIENNE PRZESYLANE Z FLASH/////////////////////////////////
$wootext   = $this->getRequestParameter('wootext'); //$_POST['wootext'];
$flashrozgrywkaid = $this->getRequestParameter('game_count');
$do_pliku = $this->getRequestParameter('wykres');
$czas   = $this->getRequestParameter('czas'); //$_POST['czas'];
/////////////////////////////ZMIENNE PRZESYLANE Z FLASH KONIEC/////////////////////////////

////////////////////////////////ZMIENNE PRZESYLANE Z SYMPHONY//////////////////////////////
$username= $this->getUser()->getGuardUser()->getId(); //'default_user';

$game = Doctrine_Core::getTable('Game')->find($this->getRequestParameter('id'));
 
$gamename=$game->getNameSlug(); //'DEFAULT_GAMENAME';

    $result_id = Doctrine_Core::getTable('GameResult')
      ->createQuery('r')
      ->select('r.*')
      ->where('r.user_id = ?', $username)
      ->addWhere('r.games_id = ?', $game->getId())
      ->addWhere('r.rozgrywka_id = ?', $flashrozgrywkaid)
      ->addOrderBy('r.end_date DESC')  
      ->limit(1)
      ->fetchOne();      

    // dla windows
    // $filename = 'C:\wamp\www\edmuchawka\www\games\score\result_'.date("dmY").'_us'.$username.'_roz'.$flashrozgrywkaid.'_res'.$result_id.'.csv';
$filename = $_SERVER["DOCUMENT_ROOT"].'/games/score/result_'.date("dmY").'_us'.$username.'_roz'.$flashrozgrywkaid.'_res'.$result_id.'.csv';
////////////////////////////ZMIENNE PRZESYLANE Z SYMPHONY KONIEC///////////////////////////


$wootext   = stripslashes($wootext);

 $dobazy = new GameResult();
 $dobazy->user_id = $this->getUser()->getGuardUser()->getId();
 $dobazy->end_date = date("Y-m-d H:i:s", time());
 $dobazy->start_date = date("Y-m-d H:i:s", time()-$czas);
 $dobazy->result_xml = $filename;
 $dobazy->score = $wootext;
 $dobazy->score_max = $czas;
 $dobazy->rozgrywka_id = $flashrozgrywkaid;
 $dobazy->games_id = $game->getId();
 $dobazy->save();

    
/*
$back=$wootext;

////////////////////////////////KONSTRUKCJA WEZLA XML//////////////////////////////
$wootext = '
		<'.$gamename.'> 
			<CZAS_GRY>'.$czas.'</CZAS_GRY> 
			<WYNIK>'.$wootext.'</WYNIK> 
			<DATA>'.time().'</DATA>          
		</'.$gamename.'> 
	</GAME>';
/////////////////////////////KONSTRUKCJA WEZLA XML KONIEC//////////////////////////





if (is_writable($filename)) {


    if (!$handle = fopen($filename, 'r+')) {
        // echo "Cannot open file ($filename)";
        // exit;
    }
	$plik_xml = fread($handle, filesize($filename)-11);


    if (fwrite($handle, $wootext) === FALSE) {
        //echo "Cannot write to file ($filename)";
        //exit;
    }

   // echo "Success, wrote ($wootext) to file ($filename)";

    fclose($handle);

} else {
    //echo "The file $filename is not writable, or doesn't exist";
	$wootext = '<?xml version="1.0"?> 
	<?xml-stylesheet type="text/xsl" href="xsl.xsl"?> 
	<GAME> 
		<TYTUL> 
			<CZAS_GRY>'.$czas.'</CZAS_GRY> 
			<WYNIK>'.$back.'</WYNIK> 
			<DATA>'.time().'</DATA>          
		</TYTUL> 
	</GAME>';
	$uchwyt = fopen($filename, "wb");
	fwrite($uchwyt, $wootext);
    fclose($uchwyt);

}

     
  */
    if (is_writable($filename)) {
    
    
        if (!$handle = fopen($filename, 'r+')) {
            // echo "Cannot open file ($filename)";
            // exit;
        }
    	$plik_xml = fread($handle, filesize($filename)-11);
    
    
        if (fwrite($handle, $wootext) === FALSE) {
            //echo "Cannot write to file ($filename)";
            //exit;
        }
    
       // echo "Success, wrote ($wootext) to file ($filename)";
    
        fclose($handle);
    
    } else {
        //echo "The file $filename is not writable, or doesn't exist";
    	$wootext = $do_pliku;
    	$uchwyt = fopen($filename, "wb");
    	fwrite($uchwyt, $wootext);
        fclose($uchwyt);
    
    }
  
 
      }
}
}